export default {
  namespaced: true,
  state: () => ({
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    userInfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    token: uni.getStorageSync('token') || '',
    redirectInfo: null
  }),
  mutations: {
    // 更新用户收货地址信息
    updataAddress(state, address) {
      state.address = address
      this.commit('m_user/saveStorage')
    },
    // 将用户收货地址保存到本地
    saveStorage(state) {
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    // 更新用户信息
    updataUserInfo(state, userInfo) {
      state.userInfo = userInfo
      this.commit('m_user/saveStorageUserInfo')
    },
    // 将用户信息保存到本地
    saveStorageUserInfo(state) {
      uni.setStorageSync('userinfo', JSON.stringify(state.userInfo))
    },
    // 更新 token 数据
    upDataToken(state, token) {
      state.token = token
      this.commit('m_user/saveStoryageToken')
    },
    // 将 token 保存到本地
    saveStoryageToken(state) {
      uni.setStorageSync('token', state.token)
    },
    // 保存购物车结算路径
    updataRedirectInfo(state, info) {
      state.redirectInfo = info
    }
  },
  getters: {
    addrstr(state) {
      if(!state.address.userName) return ''
      const {provinceName, cityName, countyName, detailInfo} = state.address
      return provinceName+cityName+countyName+detailInfo
    }
  }
}